package com.songlanyun.modules.article.model.vo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.songlanyun.common.drive.ValueObject;
import com.songlanyun.common.enums.ArticleConstant;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.utils.RedisKeys;
import com.songlanyun.common.utils.RedisUtils;
import com.songlanyun.modules.article.entity.ArticleContent;
import com.songlanyun.modules.article.service.ArticleContentService;
import com.songlanyun.modules.comment.entity.UserComment;
import com.songlanyun.modules.comment.service.UserCommentService;
import com.songlanyun.modules.like.service.LikeRecordService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.redis.core.BoundHashOperations;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章表
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-02-27 15:36:01
 */
@Data
public class ArticleVO implements ValueObject {

    /**
     *
     */
    private Long id;
    /**
     * 文章标题
     */
    private String title;
    /**
     * 文章封面
     */
    private String cover;

    /**
     * 课程ID
     */
    private Long courseId;

    /**
     * 文章类型
     * 0：图文
     * 1：视频
     */
    private ArticleConstant.ArticleType articleType;

    /**
     * 是否显示
     */
    private Boolean isShow;
    /**
     * 是否推荐
     */
    private Boolean isRecommend;

    /**
     * 上下架
     */
    private Boolean added;

    /**
     * 创建人ID
     */
    private Long creator;

    /**
     * 内容
     */
    private String content;

    public void loadContent() {
        ArticleContentService service = (ArticleContentService) SpringContextUtil.getBean("articleContentService");
        ArticleContent content = service.getByAid(this.getId());
        if (content != null) {
            this.content = content.getContent();
            this.mediaAddress = content.getMediaAddress();
            this.mediaSize = content.getMediaSize();
            this.duration = content.getDuration();
        }
    }

    /**
     * 视频地址
     */
    private String mediaAddress;
    /**
     * 文件大小
     */
    private Long mediaSize;
    /**
     * 播放时长（单位秒）
     */
    private Integer duration;


    /**
     * 初始阅读人数
     */
    private Integer initBrowseNum;
    /**
     * 初始点赞人数
     */
    private Integer initLikeNum;
    /**
     * 初始分享人数
     */
    private Integer initShareNum;

    private Date createTime;
    /**
     * 修改时间
     */
    private Date updateTime;

    /**
     * 浏览人数
     */
    private Integer browseNum;

    /**
     * 点赞人数
     */
    private Integer likeNum;

    /**
     * 分享人数
     */
    private Integer shareNum;

    /**
     * 评论人数
     */
    private Integer commentNum;

    /**
     * 浏览总数
     */
    private Integer browseSum;

    /**
     * 评论总数
     */
    private Integer commentSum;

    /**
     * 分享总数
     */
    private Integer shareSum;


    /**
     * 所有评论
     */
    private List<UserComment> userCommentList;

    public void loadUserCommentList() {
        UserCommentService service = (UserCommentService) SpringContextUtil.getBean("userCommentService");
        this.userCommentList = service.listByOpusId(this.getId(), CommentConstant.CommentModelType.ARTICLE);
    }

    /**
     * 是否点赞
     */
    private Boolean isLike;

    public void loadIsLike(Long userId) {
        LikeRecordService service = (LikeRecordService) SpringContextUtil.getBean("likeRecordService");
        //先查redis，再查数据库
        RedisUtils redisUtils = (RedisUtils) SpringContextUtil.getBean("redisUtils");
        BoundHashOperations<String, String, Object> data = redisUtils.boundHashOps(RedisKeys.BbsKey.LIKE_ARTICLE_KEY.getKey());
        HashMap<String, Object> userMap = (HashMap<String, Object>) data.get(String.valueOf(this.getId()));
        String userIdStr = String.valueOf(userId);
        //查询redis是否点赞
        if (userMap != null) {
            if (userMap.containsKey(userIdStr)) {
                this.isLike = true;
            }else {
                this.isLike = service.isLike(userId, OpusConstant.ModelType.ARTICLE, getId());
            }
            //统计redis中所有点赞数
            int redisLikeNum = userMap.keySet().size();
            this.likeNum += redisLikeNum;
        } else {
            //查询数据库是否点赞
            this.isLike = service.isLike(userId, OpusConstant.ModelType.ARTICLE, getId());
        }



    }

}
